package com.starbucks.opencase.construct;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daOnHook;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.modualr.moPetOnHook;
import com.starbucks.opencase.tool.toolUserWallet;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Timer;
import java.util.TimerTask;

@Component
public class constructOnHook {
    @Autowired
    private toolUserWallet toolUserWallet;
    @Autowired
    private funSession funSession;
    @PostConstruct
    public void start(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                ObjectMapper om=new ObjectMapper();
                for(moPetOnHook mo: daOnHook.pets){
                    if(mo.getEnergy()+daOnHook.addEnergy>daOnHook.maxEnergy)continue;
                    toolUserWallet.addEnergy(mo.getUid(),daOnHook.addEnergy);
                    long energy=toolUserWallet.queryEnergy(mo.getUid());
                    ObjectNode on=om.createObjectNode();
                    on.put(enumCMKey.mark.name(), enumCMMark.expendEnergy.ordinal());
                    on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
                    on.put(enumCMKey.status.name(), true);
                    on.put(enumCMKey.expendEnergy.name(), -daOnHook.addEnergy);
                    on.put(enumCMKey.energy.name(), energy+daOnHook.addEnergy);
                    mo.setEnergy(mo.getEnergy()+daOnHook.addEnergy);
                    funSession.sendMessage(funSession.getIdOfUId(mo.getUid()), on);
                }
            }
        },0,600_000);
    }
}
